PREFIX  = /usr/local
ARCH    = h8300-elf
BINDIR  = $(PREFIX)/bin
ADDNAME = $(ARCH)-

AR      = $(BINDIR)/$(ADDNAME)ar
AS      = $(BINDIR)/$(ADDNAME)as
CC      = $(BINDIR)/$(ADDNAME)gcc
LD      = $(BINDIR)/$(ADDNAME)ld
NM      = $(BINDIR)/$(ADDNAME)nm
OBJCOPY = $(BINDIR)/$(ADDNAME)objcopy
OBJDUMP = $(BINDIR)/$(ADDNAME)objdump
RANLIB  = $(BINDIR)/$(ADDNAME)ranlib
STRIP   = $(BINDIR)/$(ADDNAME)strip

H8WRITE = ../../../kz_h8write/kz_h8write
H8WRITE_SERDEV = com4

OBJS    = vector.o startup.o main.o interrupt.o intr.o
OBJS   += lib.o serial.o xmodem.o elf.o dram.o delay.o

TARGET  = kzload

CFLAGS  = -Wall -mh -nostdinc -nostdlib -fno-builtin
#CFLAGS += -Wno-pointer-sign
#CFLAGS += -mrelax
#CFLAGS += -mint32
CFLAGS += -I.
#CFLAGS += -g
CFLAGS += -Os
#CFLAGS += -fomit-frame-pointer
CFLAGS += -DKZLOAD

ifdef B9600
TARGET = kzload_9600bps
CFLAGS += -DSERIAL_BITRATE=9600
endif

ifdef B38400
TARGET = kzload_38400bps
CFLAGS += -DSERIAL_BITRATE=38400
endif

LFLAGS  = -static -T ld.scr -L.

.SUFFIXES: .c .o
.SUFFIXES: .s .o
.SUFFIXES: .S .o

all :       image

$(TARGET) : $(OBJS)
			$(CC) $(OBJS) -o $(TARGET) $(CFLAGS) $(LFLAGS)
			cp $(TARGET) $(TARGET).elf
			readelf -a $(TARGET).elf > readelf.out
			$(STRIP) $(TARGET)

.c.o :      $<
			$(CC) -c $(CFLAGS) $<

.s.o :      $<
			$(CC) -c $(CFLAGS) $<

.S.o :      $<
			$(CC) -c $(CFLAGS) $<

$(TARGET).mot : $(TARGET)
				$(OBJCOPY) -O srec $(TARGET) $(TARGET).mot

image :     $(TARGET).mot

write :
			$(H8WRITE) $(TARGET).mot 20 $(H8WRITE_SERDEV)

clean :
			rm -f $(OBJS) $(TARGET) $(TARGET).elf $(TARGET).mot readelf.out
